<form id="reject-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
    <div class="form-group">
        <label for="c-reject_reason" class="control-label col-xs-12 col-sm-2">{:__('拒绝原因')}:</label>
        <div class="col-xs-12 col-sm-8">
            <textarea id="c-reject_reason" name="reject_reason" class="form-control" rows="5" placeholder="请填写拒绝原因" data-rule="required"></textarea>
        </div>
    </div>
    <div class="form-group layer-footer">
        <label class="control-label col-xs-12 col-sm-2"></label>
        <div class="col-xs-12 col-sm-8">
            <button type="submit" class="btn btn-danger btn-embossed disabled">{:__('确认拒绝')}</button>
            <button type="button" class="btn btn-default btn-embossed" onclick="closeLayer()">取消</button>
        </div>
    </div>
</form>

<script>
function closeLayer() {
    try {
        // 尝试多种方式关闭弹窗
        if (typeof parent !== 'undefined' && parent.layer) {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        } else if (typeof layer !== 'undefined') {
            layer.closeAll();
        } else if (typeof parent !== 'undefined' && parent.Layer) {
            parent.Layer.closeAll();
        }
    } catch(e) {
        console.log('关闭弹窗失败:', e);
        // 如果所有方法都失败，尝试直接关闭窗口
        if (typeof parent !== 'undefined') {
            parent.postMessage('closeDialog', '*');
        }
    }
}

$(document).ready(function() {
    // 确保表单验证器正确初始化
    var form = $("#reject-form");
    
    // 移除disabled类，让按钮可以点击
    form.find('button[type="submit"]').removeClass('disabled');
    
    // 监听表单提交
    form.on('submit', function(e) {
        e.preventDefault();
        
        var reject_reason = $('#c-reject_reason').val().trim();
        if (!reject_reason) {
            alert('请填写拒绝原因');
            return false;
        }
        
        // 获取URL参数中的ids
        var urlParams = new URLSearchParams(window.location.search);
        var ids = urlParams.get('ids');
        
        if (!ids) {
            alert('参数错误');
            return false;
        }
        
        // 提交数据
        $.ajax({
            url: '{:url("product_audit/reject")}',
            type: 'POST',
            data: {
                ids: ids,
                reject_reason: reject_reason
            },
            dataType: 'json',
            success: function(data) {
                if (data.code == 1) {
                    // 成功后刷新父页面表格并关闭弹窗
                    if (parent && parent.$("#table").length > 0) {
                        parent.$("#table").bootstrapTable('refresh');
                    }
                    closeLayer();
                } else {
                    alert(data.msg || '操作失败');
                }
            },
            error: function() {
                alert('网络错误，请重试');
            }
        });
        
        return false;
    });
});
</script>